package com.example.demo.service.Impl;

import com.example.demo.ao.OAspAo;
import com.example.demo.mapper.OAspMapper;
import com.example.demo.service.OAspService;
import com.example.demo.vo.OAspVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OAspServiceImpl implements OAspService {

    @Autowired
    private OAspMapper aospMapper;

    @Override
    public int insertApplication(OAspAo oAspAo) {
        int i =0;
        oAspAo.setStartTime(new Date());
        Date data = null;
        OAspVo oAspVo1 = new OAspVo();
        List<OAspVo> oAspVos = aospMapper.selectStat(oAspAo);
        for (OAspVo oAspVo : oAspVos) {
            if (oAspVo.getUserId() == null){
                i = aospMapper.insertApplication(oAspAo);
            }
            if (oAspVo.getUserId() != null && "拒绝".equals(oAspVo.getActionType())){
                oAspVo1.setApplient(oAspVo.getApplient());
                oAspVo1.setReason(oAspVo.getReason());
                oAspVo1.setUserId(oAspVo.getUserId());
                oAspVo1.setCreateTime(data);
                oAspVo1.setEndTime(data);
                oAspVo1.setPeople(" ");
                oAspVo1.setStat("进行中");
                oAspVo1.setStartTime(new Date());
                oAspVo1.setActionType(" ");
                i = aospMapper.updateApplication(oAspVo1);
            }
        }


        return i;
    }

    @Override
    public List<OAspVo> selectAllApplication(OAspAo oAspAo) {
        List<OAspVo> oAspVos = aospMapper.selectAllApplication(oAspAo);
        List<OAspVo> processedList = new ArrayList<>();
        OAspVo oAspVo1 = new OAspVo();
        Date date = null; // 设置为 null
        for (OAspVo oAspVo : oAspVos) {
            if ("同意".equals(oAspAo.getActionType())) {
                oAspVo1.setApplient(oAspVo.getApplient());
                oAspVo1.setReason(oAspVo.getReason());
                oAspVo1.setStartTime(oAspVo.getStartTime());
                oAspVo1.setEndTime(date);
                oAspVo1.setStat("进行中");
                String role = peopleTransform(oAspAo);
                oAspVo1.setPeople(role);
                oAspVo1.setActionType(oAspAo.getActionType());
                oAspVo1.setCreateTime(new Date());
                oAspVo1.setUserId(oAspVo.getUserId());
                aospMapper.updateApplication(oAspVo1);
            }
            if("拒绝".equals(oAspAo.getActionType())) {
                oAspVo1.setApplient(oAspVo.getApplient());
                oAspVo1.setReason(oAspVo.getReason());
                oAspVo1.setStartTime(oAspVo.getStartTime());
                oAspVo1.setEndTime(new Date());
                oAspVo1.setStat("已完结");
                oAspVo1.setPeople(peopleTransform(oAspAo));
                oAspVo1.setActionType(oAspAo.getActionType());
                oAspVo1.setCreateTime(new Date());
                oAspVo1.setUserId(oAspVo.getUserId());
                aospMapper.updateApplication(oAspVo1);
            }
        }
        processedList.add(oAspVo1);


        return processedList;
    }


    public String peopleTransform(OAspAo oAspVo) {

        if ("001".equals(oAspVo.getUserId())) {
            return "组长";
        } else if ("000".equals(oAspVo.getUserId())) {
            return "boss";
        }
        return "普通员工";

    }
}
